package com.qiyu.mall.user.provider.service;

import com.qiyu.core.cache.redis.client.IRedisService;
import com.qiyu.mall.user.interfaces.dto.WorkerDTO;
import com.qiyu.mall.user.interfaces.service.WorkerService;
import com.qiyu.mall.user.provider.dao.WorkerDao;
import com.qiyu.mall.user.provider.entity.WorkerPO;
import com.qiyu.mall.user.provider.utils.BeanCopyUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author idea
 * @data 2020/1/23
 */
@Service
public class WorkerServiceImpl implements WorkerService {

    @Resource
    private WorkerDao workerDao;
    @Resource
    private IRedisService redisService;


    @Override
    public String createToken(Integer workerId) {
        String token = UUID.randomUUID().toString() + workerId;
        return token;
    }

    @Override
    public String login(Integer workerId, String workerPwd) {
        WorkerPO workerPO = new WorkerPO();
        workerPO.setWorkerId(workerId);
        workerPO.setPassword(workerPwd);
        WorkerPO result = workerDao.selectOne(workerPO);
        if (result != null) {
            String token = "worker:login:" + createToken(workerId);
            redisService.setObj(token, result, 60, TimeUnit.DAYS);
            return token;
        } else {
            return null;
        }
    }

    @Override
    public Boolean addOne(WorkerDTO workerDTO) {
        return null;
    }

    @Override
    public List<WorkerDTO> selectAll() {
        List<WorkerPO> workerPOList = workerDao.selectAll();
        List<WorkerDTO> workerDTOS = BeanCopyUtil.copyList(workerPOList, WorkerDTO.class);
        return workerDTOS;
    }


    @Override
    public HashMap<Integer, WorkerDTO> batchQuery(List<Integer> workerIdList) {
        List<WorkerPO> workerPOS = workerDao.batchQuery(workerIdList);
        HashMap<Integer, WorkerDTO> result = new HashMap<>(workerPOS.size());
        for (WorkerPO workerPO : workerPOS) {
            WorkerDTO workerDTO = new WorkerDTO();
            BeanUtils.copyProperties(workerPO, workerDTO);
            result.put(workerDTO.getWorkerId(), workerDTO);
        }
        return result;
    }

    public static void main(String[] args) {

    }
}
